package com.springcloudalibaba.apiAsyn.controller;

import com.springcloudalibaba.apiAsyn.entity.BaseFileEntity;
import com.springcloudalibaba.apiAsyn.service.FileService;
import com.springcloudalibaba.bclass.base.BaseResult;
import com.springcloudalibaba.bclass.base.BaseUploadResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.util.concurrent.CompletableFuture;

/**
 * @描述: 文件控制器
 * @作者: lixing
 * @日期 2020/12/12 21:10
 */
@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private FileService fileService;

    /**
     * @描述 TODO 上传文件
     * @作者 lixing
     * @日期 2019/3/30 15:51
     * @Param [
     *  baseUploadFileType 上传文件的类型，详见UploadFileTypeEnum枚举【必填】
     *  baseUploadCategory 上传文件的业务场景，例如: cover 【必填】
     *  baseUploadMultipartFiles 上传文件的MultipartFile集合，@RequestParam("file") MultipartFile[] multipartFiles 【必填】
     * ]
     */
    @PostMapping("upload")
    public BaseResult<BaseUploadResult> upload(BaseFileEntity baseEntity) throws Exception {
        CompletableFuture<BaseResult<BaseUploadResult>> uploadImageCompletableFuture = fileService.upload(baseEntity);
        CompletableFuture.allOf(uploadImageCompletableFuture).join();
        return uploadImageCompletableFuture.get();
    }

    /**
     * @描述 TODO 下载文件
     * @作者 lixing
     * @日期 2019/3/30 15:51
     * @Param [
     *  baseDownFileUrl 下载文件的地址，绝对路劲，格式：/upload/image/cover/2021-06-13/19d03fc7-a387-4ecb-ae6e-d859661770ea.png 【必填】
     *  baseDownFileDelete: 下载后是否删除该文件，true删除 false不删除 【必填】
     * ]
     */
    @PostMapping("down")
    public void down(BaseFileEntity baseEntity, HttpServletResponse response, HttpServletRequest request) throws Exception {
        fileService.down(baseEntity, request, response);
    }

}
